/**
 * @author: welling
 */
Weifox.browser={
	init:function(){
		var doc=document
		//右键菜单 
		var contextMenu = doc.getElementById("contentAreaContextMenu");
		if (contextMenu) 
			contextMenu.addEventListener("popupshowing", function(e){
				Weifox.browser.onContextMenu();
			}, false);
	},
	copyText2ClipBoard:function(text){
		const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);  
        gClipboardHelper.copyString(text);
	},
	onContextMenu:function(){
		var doc=document;
		var show=Weifox.browser.pref.getBoolPref('show_context_menu');
		var menu=doc.getElementById('weifox-context-menu');
		var pic=doc.getElementById('weifox-context-menu-pic');
		var bg=doc.getElementById('weifox-context-menu-bg');
		
		if (show) {
			menu.hidden = false;
			pic.hidden=true;
			bg.hidden=true;
			var m = gContextMenu;
			if (!m) {
				return;
			}
			if (m.hasBGImage || m.onImage) {//有图片或背景图片
				if(m.onImage){
					pic.hidden=false;
					pic.setAttribute('image',m.mediaURL);
				}else{
					bg.hidden=false;
					bg.setAttribute('image',m.bgImageURL);
				}
			}
		}else
			menu.hidden = true;
	},
	//jump true/undefined->跳转,false 不跳转到页面
    openLink: function(url,jump){
		if(!url||url.length=='')return;
        var tabbrowser = gBrowser;
        var tabs = tabbrowser.tabContainer.childNodes;
        for (var i = 0; i < tabs.length; ++i) {
            var tab = tabs[i];
            try {
                var browser = tabbrowser.getBrowserForTab(tab);
                if (browser) {
                    var doc = browser.contentDocument;
                    var loc = doc.location.toString();
                    if (loc == url) {
                        gBrowser.selectedTab = tab;
                        return;
                    }
                }
            }
            catch (e) {
            }
        }
        
        // There is no tab. open new tab...
        var tab = gBrowser.addTab(url, null, null);
		if(jump==undefined||jump)
        	gBrowser.selectedTab = tab;
    },
	getCurrentLink:function(){
		return gBrowser.contentDocument.location;
	},
	getCurrentTitle:function(){
		return gBrowser.contentDocument.title;
	},
	/**
	 * 共有以下方法可调用
	 * getBoolPref(), setBoolPref(), getCharPref(), setCharPref(), getIntPref() setIntPref(). 
	 * 含中文的值要先进行Url编码,Pref里存的是UTF-8,JS用UTF-16
	 * https://developer.mozilla.org/En/Firefox_addons_developer_guide/Using_XPCOM%E2%80%94Implementing_advanced_processes
	 * 
	 * 中文处理:
	 * this.pref.setCharPref(key, unescape(encodeURIComponent(value)));
	 * 
	 * decodeURIComponent(escape(this.pref.getCharPref(key)));
	 */
	pref:Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService).getBranch("okey007@126.com.weifox.")
};

